package org.support.small.mallms.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.support.small.mallms.contants.WebSiteURLContants;
import org.support.small.mallms.model.Ret;
import org.support.small.mallms.param.UsernamePasswordParam;
import org.support.small.mallms.service.AdminsService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping(WebSiteURLContants.PREFIX_ADMIN)
public class AdminController {
    @Resource
    private AdminsService adminsService;

    @PostMapping("login")
    public Ret<Map<String, String>> login(@Validated @RequestBody UsernamePasswordParam param, HttpServletRequest request) {
        log.info("param:{}", param);
        String token = adminsService.login(param.getUsername(), param.getPassword(), request);
        Map<String, String> tokenMap = Map.of("token", token);
        return Ret.ok(tokenMap);
    }
}
